package com.accp.service.impl;

import com.accp.entity.Hotevents;
import com.accp.mapper.HoteventsMapper;
import com.accp.service.CommentsService;
import com.accp.service.HoteventsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jack
 * @since 2021-08-03
 */
@Service
public class HoteventsServiceImpl implements HoteventsService {

	@Autowired
	HoteventsMapper hoteventsMapper;
	
	@Autowired
	CommentsService commentsService;
	
	@Override
	public IPage<Hotevents> getEventByPage(int pageIndex, int pageRow, String keyword) {
		Page pageflag = new Page(pageIndex,pageRow);
		QueryWrapper<Hotevents> qu = new QueryWrapper<Hotevents>();
		if(keyword!=null && keyword.length()>0){
			//加入筛选条件
			qu.like("keyword",keyword);
		}
		qu.orderByDesc("searchSum");
		IPage pageData = this.hoteventsMapper.selectPage(pageflag,qu);
		return pageData;
	}

	@Override
	public Hotevents fetchEvent(int id) {
		Hotevents event =  this.hoteventsMapper.selectById(id);
		if(event == null){
			//return null;
			throw new RuntimeException("该热点事件不存在");
		}
		//查找该热点事件的评论列表
		event.setComments(this.commentsService.getCommentsByHid(event.getId()));
		return event;
	}

}
